# 5个选项，200份数据。响应率和普及率
# 响应率：A选项选了70，70/200
# 普及率：A选项选了70个，
import re

import pandas as pd

from utils.report.base import convert_str_to_float_3


def dcxy_analysis(df: pd.DataFrame, groups: []):
    group_dict = dict()
    for cur_index in range(len(groups)):
        cur_o = groups[cur_index]
        items = cur_o.get('items')
        option_len = len(items)
        max_1 = option_len * df.shape[0]
        f_res = []
        for j in items:
            cur_j_name = j.get('name')
            cur_data = df[cur_j_name]
            count_ones = sum(cur_data.apply(lambda x: bool(re.match(r'^1(?!1)', str(x)))))
            inner_dict = dict()
            inner_dict['选项'] = cur_j_name
            inner_dict['n'] = count_ones
            inner_dict['响应率'] = convert_str_to_float_3(count_ones / max_1 * option_len * 100)
            inner_dict['普及率'] = convert_str_to_float_3(count_ones / max_1 * 100)
            # f_res.append({'row_key': str(cur_j_name), 'values': inner_dict})
            f_res.append(inner_dict)
        f_res.sort(key=lambda x: x['n'], reverse=True)  # 根据频数分析的结果降序排列
        group_dict[cur_o.get('name')] = f_res
    return group_dict


if __name__ == '__main__':
    pass
